package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private JwtProperties jwtProperties;

    @PutMapping
    @ApiOperation("分类修改")
    public Result categoryEdit(@RequestBody CategoryDTO dto) {
        categoryService.categoryEdit(dto);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> categoryPageQuery(CategoryPageQueryDTO dto) {
        PageResult result = categoryService.categoryPageQuery(dto);
        return Result.success(result);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用分类")
    public Result categoryStatusChange(@PathVariable Integer status, Long id) {
        categoryService.categoryStatusChange(id, status);
        return Result.success();
    }

    @PostMapping()
    @ApiOperation("新增分类")
    public Result categoryAdd(@RequestBody CategoryDTO dto) {
        categoryService.categoryAdd(dto);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result categoryDeleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type) {
        List<Category> categoryList = categoryService.listByType(type);
        return Result.success(categoryList);
    }
}
